Go: Add Herb Go FFI Bindings#830
Closed
omarluq wants to merge 2 commits into
Closed
Conversation
This PR adds Go bindings for Herb using `c-for-go` (FFI), allowing Go
applications to parse and analyze ERB templates with the same implementation
as the native C, Ruby C-Extension, C++ Emscripten WASM, Java JNI, Rust FFI,
and the C++ Node.js NAPI bindings.
Building the Go bindings requires Go 1.20+, a C compiler, and c-for-go:
```bash
cd go
make build
```
Usage:
```go
package main
import (
"fmt"
"github.com/marcoroth/herb"
)
func main() {
template := "<h1><%= title %></h1>"
// Lex
tokens := herb.Lex(template)
// Parse
ast := herb.Parse(template, nil)
// Extract Ruby
ruby := herb.Extract(template, 0)
}
```
The Go bindings use a minimal wrapper header approach (similar to Java's JNI
bindings) to avoid exposing prism dependencies while linking against both
`libherb.a` and `libprism.a`
Add `InspectTokens()` function to provide formatted token output similar to Rust/Java bindings: - Extend herb_go.h with token structures (position_T, range_T, location_T, token_T) - Add array and token helper functions (hb_array_size, hb_array_get, token_type_to_string) - Implement `token_helpers.go` with `InspectTokens()` user-facing API
Author
|
Will reopen later after multi backend PR |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR adds Go bindings for Herb using
c-for-go(FFI), allowing Goapplications to parse and analyze ERB templates with the same implementation
as the native C, Ruby C-Extension, C++ Emscripten WASM, Java JNI, Rust FFI,
and the C++ Node.js NAPI bindings.
Building the Go bindings requires Go 1.20+, a C compiler, and c-for-go:
cd go make buildUsage:
The Go bindings use a minimal wrapper header approach (similar to Java's JNI
bindings) to avoid exposing prism dependencies while linking against both
libherb.aandlibprism.a